//---------------------------------------------------------------------------
// Author : Nguyen Duc Khanh
// Created Date : Saturday, December 15, 2007
//---------------------------------------------------------------------------


using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using vs.Core;

namespace Forum
{
    public class ForumController
    {
        #region Constructor
        public ForumController() { }
        #endregion

        #region Function
        private const string INSERT_FORUM_FORUM = "INSERT INTO FORUM_Forum (ForumID, ForumName, CategoryID, Description, OrderIndex, TotalTopic, TotalView, TotalReply) "
         + " VALUES (@ForumID, @ForumName, @CategoryID, @Description, @OrderIndex, @TotalTopic, @TotalView, @TotalReply)";
        public int Insert(ForumInfo forum)
        {
            int result = 0;
            try
            {
                DataParameter[] param = new DataParameter[8];
                param[0] = new DataParameter("@ForumID", forum.ForumID);
                param[1] = new DataParameter("@ForumName", forum.ForumName);
                param[2] = new DataParameter("@CategoryID", forum.CategoryID);
                param[3] = new DataParameter("@Description", forum.Description);
                param[4] = new DataParameter("@OrderIndex", forum.OrderIndex);
                param[5] = new DataParameter("@TotalTopic", forum.TotalTopic);
                param[6] = new DataParameter("@TotalView", forum.TotalView);
                param[7] = new DataParameter("@TotalReply", forum.TotalReply);
                result = DataFactory.Execute(INSERT_FORUM_FORUM, param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        private const string UPDATE_FORUM_FORUM = "UPDATE FORUM_Forum SET "
             + " ForumName = @ForumName, "
             + " CategoryID = @CategoryID, "
             + " Description = @Description, "
             + " OrderIndex = @OrderIndex "
             + " WHERE (ForumID = @ForumID)";
        public int Update(ForumInfo forum)
        {
            int result = 0;
            try
            {
                ArrayList param = new ArrayList();
                param.Add(new DataParameter("@ForumName", forum.ForumName));
                param.Add(new DataParameter("@CategoryID", forum.CategoryID));
                param.Add(new DataParameter("@Description", forum.Description));
                param.Add(new DataParameter("@OrderIndex", forum.OrderIndex));
                param.Add(new DataParameter("@ForumID", forum.ForumID));
                result = DataFactory.Execute(UPDATE_FORUM_FORUM, param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        private const string DELETE_FORUM_FORUM = "DELETE FROM FORUM_Forum WHERE (ForumID = @ForumID)";
        public int Delete(string ForumID)
        {
            int result = 0;
            try
            {
                DataParameter param = new DataParameter("@ForumID", ForumID);
                result = DataFactory.Execute(DELETE_FORUM_FORUM, param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public ForumInfo Converting(DataRow drFORUM_Forum)
        {
            ForumInfo result = null;
            try
            {
                result = new ForumInfo();
                result.ForumID = (string)drFORUM_Forum["ForumID"];
                result.ForumName = (string)drFORUM_Forum["ForumName"];
                result.CategoryID = (string)drFORUM_Forum["CategoryID"];
                result.Description = (string)drFORUM_Forum["Description"];
                result.OrderIndex = (int)drFORUM_Forum["OrderIndex"];
                result.TotalTopic = (int)drFORUM_Forum["TotalTopic"];
                result.TotalView = (int)drFORUM_Forum["TotalView"];
                result.TotalReply = (int)drFORUM_Forum["TotalReply"];
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        private const string LOAD_FORUM_FORUM = "SELECT * FROM FORUM_Forum WHERE (ForumID = @ForumID)";
        public ForumInfo Load(string ForumID)
        {
            ForumInfo result = null;
            try
            {
                DataParameter[] param = new DataParameter[1];
                param[0] = new DataParameter("@ForumID", ForumID);
                DataTable dt = DataFactory.SelectTable(LOAD_FORUM_FORUM, param);
                if (dt.Rows.Count > 0) result = Converting(dt.Rows[0]);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        private const string LOAD_LIST_FORUM_FORUM = "SELECT * FROM FORUM_Forum";
        public DataTable LoadTable()
        {
            DataTable result = null;
            try
            {
                result = DataFactory.SelectTable(LOAD_LIST_FORUM_FORUM);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        #endregion

    }
}
